#!/usr/bin/python

import pygame
import MoveableEntity
import game_engine.GameEngine

ATTACK_DAMAGE = 50

# A special entity class that represents the player
class PlayerEntity(MoveableEntity.MoveableEntity):

    # Obvious
    def __init__(self):
        super(PlayerEntity, self).__init__("PLAYER")
        self.m_renderEngine = game_engine.GameEngine.getGameEngine().getRenderEngine()
        self.m_Inventory = [None for i in range(10)]

    # Try to move this object to the tile specified (note that the object will not cross the intervening tiles!)
    def tryWarpTo(self, newX, newY, timeToMovems):
        # Call parent class method
        if not(super(PlayerEntity, self).tryWarpTo(newX, newY, timeToMovems)):
            return False

        # Update viewport
        (viewX, viewY) = self.m_renderEngine.getViewDimensions()
        offsetX=int(viewX / 2)
        offsetY=int(viewY / 2)
        screenX=newX * self.m_tileSize - offsetX
        screenY=newY * self.m_tileSize - offsetY
        if screenX < 0:
            screenX = 0
        if screenY < 0:
            screenY = 0
        self.m_renderEngine.setViewTargetUpperLeft(screenX, screenY)


        return True


    # Called when an entity attempts to interact with this entity by bumping into it
    def interactBumped(self, otherEntity):
        print "A %s hit you for 1 damage" % (otherEntity.getName())
        self.setCurrentHealth(self.m_currentHealth - 1, otherEntity)
        print "You now have %d heath." % (self.m_currentHealth)
        return True

    def attack(self):
        if self.m_attacking == False:
            self.m_attacking = True

            if self.m_direction == "right":
                for y in range(self.m_sizeHeight):
                    self.attackTileOccupents((self.m_positionX + self.m_sizeWidth), (self.m_positionY + y))
            if self.m_direction == "left":
                for y in range(self.m_sizeHeight):
                    self.attackTileOccupents((self.m_positionX - 1), (self.m_positionY + y))
            if self.m_direction == "up":
                for x in range(self.m_sizeWidth):
                    self.attackTileOccupents((self.m_positionX + x), (self.m_positionY - 1))
            if self.m_direction == "down":
                for x in range(self.m_sizeWidth):
                    self.attackTileOccupents((self.m_positionX + x), (self.m_positionY + self.m_sizeHeight))
                
    def attackTileOccupents(self, tileX, tileY):
        global ATTACK_DAMAGE
        collisionMap = game_engine.GameEngine.getGameEngine().getLevelManager().getEntityCollisionMap()
        tileOccupents = collisionMap.getTileOccupants(tileX, tileY)
        for occupent in tileOccupents:
            if type(occupent).__name__ == 'FollowMonster':
                occupent.setCurrentHealth(occupent.getCurrentHealth() - ATTACK_DAMAGE)
                if occupent.isAlive() == False:
                    occupent.stopFollowing()
                    game_engine.GameEngine.getGameEngine().getEntityManager().unregisterEntity(occupent)
                else:
                    consoleText = "%s now has %d health" % (occupent.m_name, occupent.m_currentHealth)
                    game_engine.GameEngine.getGameEngine().getUIManager().addTextToConsole(consoleText)
            else:
                game_engine.GameEngine.getGameEngine().getUIManager().addTextToConsole(type(occupent).__name__)
            
                    
    def addToInventory(self,Entity):
        i=0
        for i in range(len(self.m_Inventory)):
            if(self.m_Inventory[i] == None):
                break

        self.m_Inventory[i] = (Entity)
        game_engine.GameEngine.getGameEngine().getUIManager().addTextToConsole("Picked up a %s" % (Entity.getName()))

    def removeFromInventory(self,slot):
        print self.m_Inventory
        self.m_Inventory[slot] = None
        return True

    def getFullInventory(self):
        return self.m_Inventory

    def getInventorySlot(self,slot):
        #lets make sure we have that item in our inventory
        if(len(self.m_Inventory < slot)):
                return None
        return self.m_Inventory[slot]

    def useInventoryItem(self,slot):
        if(len(self.m_Inventory) == 0):
            return 
        print self.m_Inventory[slot]
        if not (self.m_Inventory[slot] == None):
            self.m_Inventory[slot].use(self)
            self.removeFromInventory(slot)

    def stopAttack(self):
        self.m_attacking = False
        
        
        
